package com.zfh.demo.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zfh.demo.blog.entity.Student;
import com.zfh.demo.blog.mapper.StudentMapper;
import com.zfh.demo.blog.service.StudentService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周孚环
 * @since 2020-07-02
 */
@Controller
@RequestMapping("/blog/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentMapper studentMapper;


    /**
     * 模糊查询
     */
    @RequestMapping("/selectLike")
    public String selectLike(@RequestParam("name") String name,Model model){
        List<Student> list = studentService.selectLike(name);
        model.addAttribute("list",list);
        return "show";
    }

    /**
     * 添加操作
     */
    @RequestMapping("/addStudent")
    public String addStudent(){
        return "add";
    }

    @RequestMapping("/addStudents")
    public String addStudents(Student student){
        this.studentService.addStudent(student);
        return "redirect:/blog/student/findAll";
    }

    /**
     * 删除操作
     */
    @RequestMapping("/deleteStudent")
    public String deleteStudent(Long id){
        this.studentService.daleta(id);
        return "redirect:/blog/student/findAll";
    }

    /**
     * 更新操作
     * @param student
     * @return
     */
    @RequestMapping("/update")
    public String update(Student student){
        try{
            studentService.update(student);
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
        return "redirect:/blog/student/findAll";
    }

    /**
     * 预更新
     * @return
     */
    @RequestMapping("/perUpdateStudent")
    public String perUpdateStudent(Integer id,Model model){
        Student perUpdateStudent = studentService.perUpdateStudent(id);
        model.addAttribute("perUpdateList",perUpdateStudent);
        return "update";
    }

    /**
     * 带条件分页
     * @param name 模糊查询条件
     * @param pageIndex ：页码
     * @param pageSize  ：每页展示数
     * @param model
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "name",defaultValue = "") String name,@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                          @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize,
                          Model model
    ){

        model.addAttribute("name",name);
        List<Student> list = studentService.findAll(name,pageIndex,pageSize);
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        model.addAttribute("list",list);
        model.addAttribute("pageInfo",pageInfo);
        return "show";
    }

    /**
     * 查询所有，跳转到第几页
     * @param model ：
     * @param num   ：跳转第几页
     * @return
     */
    @RequestMapping("/findStudentAll")
    public String findStudentAll(Model model,Integer num
    ){
        if (num==null){
            PageHelper.startPage(0,5);
        }else{
            PageHelper.startPage(num,5);
        }
        try {
            List<Student> list = this.studentService.findStudentAll();
            PageInfo<Student> pageInfo= new PageInfo<Student>(list);
            model.addAttribute("list",list);
            model.addAttribute("pageInfo",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
        return "show";
    }


}

